package com.dxj.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxj.domain.Permission;
import com.dxj.mapper.PermissionMapper;
import com.dxj.request.PermissionDTO;
import com.dxj.response.R;
import com.dxj.service.IPermissionService;
import io.github.linpeilie.Converter;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Set;

/**
 * @author dxj
 * @Description 权限服务实现类
 * @date 2024/7/29 20:44
 */
@Service
public class PermissionImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {
    @Resource
    private Converter converterMapper;

    /**
     * 通过用户id查询用户权限
     *
     * @param userId
     * @return
     */
    @Override
    public Set<String> getUserPermissionById(Long userId) {
        return baseMapper.getUserPermissionById(userId);
    }

    /**
     * 新增权限
     *
     * @param permissionDTO
     * @return
     */
    @Override
    public R insertPermission(PermissionDTO permissionDTO) {
        //判断是否重复
        Long count = baseMapper.selectCount(new LambdaQueryWrapper<Permission>().eq(Permission::getPermissionName, permissionDTO.getPermissionName()));
        if (count > 0) {
            return R.fail("重复添加权限");
        }
        Permission permission = converterMapper.convert(permissionDTO, Permission.class);
        baseMapper.insert(permission);
        return R.success();
    }
}
